#include "crecordsdialog.h"
#include "qscrollbar.h"
#include "ui_crecordsdialog.h"
#include"save.h"
#include"../widget/crecordswid.h"

#include <usermgr.h>

CRecordsDialog::CRecordsDialog(const QString& chatRoom, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::CRecordsDialog)
    , _chatRoom(chatRoom)
{
    ui->setupUi(this);
    ui->listWidget->verticalScrollBar()->setSingleStep(20);

    _notLoaded = Save::GetInstance()->QueryChatSum(_chatRoom);

    connect(ui->listWidget->verticalScrollBar(), &QScrollBar::valueChanged, this, [this](int value){
        if (value != 0) return;
        loadChatRecord();
    });
    connect(ui->lineEdit, &QLineEdit::textChanged, this, [this](const QString &text){
        if (!text.isEmpty() || _isSearch) return;
        ui->listWidget->clear();
        _notLoaded = Save::GetInstance()->QueryChatSum(_chatRoom);
        _pioneer = 0;
        loadChatRecord();
    });

    loadChatRecord();
    ui->listWidget->scrollToBottom();
}

CRecordsDialog::~CRecordsDialog()
{
    delete ui;
}

void CRecordsDialog::loadChatRecord()
{
    if (_notLoaded == 0){
        return;
    }
    std::vector<Message> msgs(_notLoaded < 20 ? _notLoaded : 20);
    int minId = _pioneer == 0 ? INT_MAX : _pioneer;
    int id = Save::GetInstance()->QueryChatRecords(_chatRoom, minId, msgs);
    if (id == 0){
        qDebug() << "获取聊天记录错误";
        return;
    }
    _pioneer = id;
    _notLoaded -= msgs.size();

    QString me{UserMgr::GetInstance()->getName()};

    int max1 = ui->listWidget->verticalScrollBar()->maximum();
    for (const Message& msg : msgs){
        if (msg._is_me) addChatRecord(me, msg._message, msg._time.toString("yy-MM-dd HH:mm"));
        else addChatRecord(_chatRoom, msg._message, msg._time.toString("yy-MM-dd HH:mm"));
    }
    ui->listWidget->verticalScrollBar()->setValue(ui->listWidget->verticalScrollBar()->maximum() - max1);
}

void CRecordsDialog::addChatRecord(const QString &sender, const QString &text, const QString& time)
{
    CRecordsWid *f = new CRecordsWid(sender, text, time, ui->listWidget);
    QListWidgetItem *item = new QListWidgetItem();
    connect(f, &CRecordsWid::InterfaceUpdate, this, [item](QSize size){
        size.setHeight(size.height() + 20);
        item->setSizeHint(size);
    });
    item->setSizeHint(f->sizeHint());

    ui->listWidget->insertItem(0, item);
    ui->listWidget->setItemWidget(item, f);
}

void CRecordsDialog::on_pushButton_clicked()
{
    if (ui->lineEdit->text().isEmpty()) return;
    std::vector<Message> msgs;
    if (!Save::GetInstance()->SearchChatRecords(_chatRoom, ui->lineEdit->text(), msgs)) return;
    ui->listWidget->clear();

    QString me{UserMgr::GetInstance()->getName()};
    for (const Message& msg : msgs){
        if (msg._is_me) addChatRecord(me, msg._message, msg._time.toString("yy-MM-dd HH:mm"));
        else addChatRecord(_chatRoom, msg._message, msg._time.toString("yy-MM-dd HH:mm"));
    }
    _isSearch = false;
}

void CRecordsDialog::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
        on_pushButton_clicked();
    }
    QWidget::keyPressEvent(event);
}

